AWS EC2 部署 django 项目

关于 EC2 部署 django 项目以及 pip DistributionNotFound Error。

EC2 部署 django 项目

其实不只是 django,大多数的 server 部署到 EC2 上要修改的地方其实都差不多。
三个注意点:

  1. 确保 EC2 的端口开了
  2. 修改 django project 的 settings.py,把 EC2 的 public dns 加入 ALLOWED_HOSTS
    ALLOWED_HOSTS = ['ec2-52-14-223-164.us-east-2.compute.amazonaws.com']
    
  3. 开启 Server,注意监听地址
    python manage.py runserver 0.0.0.0:8000
    

EC2 pip DistributionNotFound Error

顺便提一下 pip,好好的在安装依赖的过程中,突然出现了错误,pip 完全用不了了…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ sudo pip --version
Traceback (most recent call last):
File "/usr/bin/pip", line 5, in <module>
from pkg_resources import load_entry_point
File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3138, in <module>
@_call_aside
File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3124, in _call_aside
f(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3151, in _initialize_master_working_set
working_set = WorkingSet._build_master()
File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 663, in _build_master
return cls._build_from_requirements(__requires__)
File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 676, in _build_from_requirements
dists = ws.resolve(reqs, Environment())
File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 849, in resolve
raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'pip==6.1.1' distribution was not found and is required by the application

起因是某小哥突然更新了 pip… 太忧伤……小哥 Google 了若干帖子,然而事实证明,bug 这种事,有时就算试过了所有网上的解决方案也不一定能 de 出来,哪怕是卸载了重来。。

当然收获还是有的,Stack overflow 上有个解释不错

On upgrading pip on the system, as the root user, you actually overwrite your system PIP program, and are subject to severe problem when further installing Python packages for your Linux system (with yum/dnf). The correct way to work with this is to create a virtualenv as a user, and on that virtualenv you upgrade PIP. Isolated from the system Python installation. Anything remotely serious you will want to do with Python on this machine should be running at least Python 2.7 anyway - or 3.6 if it is Python 3 compatible. (Your system Python is 2.6 and you have a Python2. on /usr/local which might conflict, exactly depending on the order of PATH as you found out).

Anyway,应该是版本冲突 + 配置的问题,于是先看一下 pip 文件

1
2
3
$ which pip
/usr/local/bin/pip
$ vim /usr/local/bin/pip

1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/python2.7
# EASY-INSTALL-ENTRY-SCRIPT: 'pip==6.1.1','console_scripts','pip'
__requires__ = 'pip==6.1.1'
import re
import sys
from pkg_resources import load_entry_point
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(
load_entry_point('pip==6.1.1', 'console_scripts', 'pip2.7')()
)

要知道 pip9.0.1 其实已经有了,所以手动把所有 pip==6.1.1 改成 pip==9.0.1,然而并没有起作用。

那就直接追踪下问题出现的目录吧

1
2
3
4
5
6
7
8
9
10
11
12
$ cd /usr/local/lib/python2.7/site-packages/pkg_resources/
$ ls
__init__.py __init__.pyc
$ cd ..
$ ls
appdirs-1.4.3.dist-info djangorestframework-3.6.2.dist-info olefile-0.44.egg-info pip-9.0.1-py2.7.egg setuptools
appdirs.py easy-install.pth OleFileIO_PL.py pkg_resources setuptools-34.3.3.dist-info
appdirs.pyc easy_install.py OleFileIO_PL.pyc pyparsing-2.2.0.dist-info six-1.10.0.dist-info
django easy_install.pyc packaging pyparsing.py six.py
Django-1.10.6.dist-info markdown packaging-16.8.dist-info pyparsing.pyc six.pyc
django_filter-1.0.2.dist-info Markdown-2.6.8.egg-info pip README
django_filters olefile pip-9.0.1.dist-info rest_framework

然后注意了,我们确实有 pip-9.0.1,然而我们并木有 pip2.7 这个东西,有的只是 pip!所以 =>

1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/python2.7
# EASY-INSTALL-ENTRY-SCRIPT: 'pip==9.0.1','console_scripts','pip'
__requires__ = 'pip==9.0.1'
import re
import sys
from pkg_resources import load_entry_point
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(
load_entry_point('pip==9.0.1', 'console_scripts', 'pip')()
)

然后,问题解决~~

徐阿衡 wechat
欢迎关注:徐阿衡的微信公众号
客官,打个赏呗~